package com.sduept.nwld.kpi.manager;

import com.sduept.core.dao.SuperDAO;
import com.sduept.nwld.dataserver.enums.ExceptionType;
import com.sduept.nwld.dataserver.model.Logger;
import com.sduept.nwld.kpi.datasource.KpiQueryCondition;
import com.sduept.nwld.kpi.model.KpiAbstractObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.xml.sax.SAXException;

import java.net.ConnectException;
import java.sql.SQLException;
import java.util.Date;

//@Service 
/**
 * @author wk
 *2020-02-29 去除 ejb管理，该类目前在项目中未曾使用
 */
public class ExceptionHandlerService {
	@Autowired
	private SuperDAO dao;
	public void logException(KpiAbstractObject ri,Exception e){
		try{
		Logger loger = new Logger();
			loger.setObjId(ri.getId());
			loger.setName(ri.getName());
			loger.setMessage(e.getMessage());
			loger.setTriggerDate(new Date());
			if(e instanceof ConnectException){
				loger.setExceptionType(ExceptionType.ConnectException.getLabel());
			} else if(e instanceof SAXException){
				loger.setExceptionType(ExceptionType.SAXException.getLabel());
			} else if(e instanceof SQLException){
				loger.setExceptionType(ExceptionType.SQLException.getLabel());
			}
			dao.create(loger, Logger.class);
		} catch (Exception e1){
			e.printStackTrace();
			e1.printStackTrace();
		}
	}
	public void logResponseException(KpiAbstractObject ri,String e)  throws Exception{
		try{
			Logger loger = new Logger();
			loger.setObjId(ri.getId());
			loger.setName(ri.getName());
			if(e != null){
				loger.setMessage(e.toString());
			} else {
				loger.setMessage("未定义的错误代码："+e);
			}
			loger.setTriggerDate(new Date());
			loger.setExceptionType(ExceptionType.ResponseException.getLabel());
			dao.create(loger, Logger.class);
		} catch (Exception e1){
			e1.printStackTrace();
		}
	}
	public void logHeaderException(KpiQueryCondition q,Exception e){
		try{
			Logger loger = new Logger();
			loger.setObjId(q.getId());
			loger.setName(q.getName());
			loger.setMessage(e.getMessage());
			loger.setTriggerDate(new Date());
			if(e instanceof ConnectException){
				loger.setExceptionType(ExceptionType.ConnectException.getLabel());
			} else if(e instanceof SAXException){
				loger.setExceptionType(ExceptionType.SAXException.getLabel());
			} else if(e instanceof SQLException){
				loger.setExceptionType(ExceptionType.SQLException.getLabel());
			}
			dao.create(loger, Logger.class);
		} catch (Exception e1){
			e.printStackTrace();
			e1.printStackTrace();
		}
		
	}
}
